<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>后台管理系统</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      body {
        /* background-image: url("./img/one.png"); */
        /* background-size: cover; */
        background-color: black;
        color: #fff;
      }
      #app {
        margin: 40px;
      }
      #app .app-top {
        border: 5px solid #fff;
        height: 50px;
      }
      #app .app-top button {
        margin-left: 20px;
      }
      .app-middle {
        border: 5px solid #fff;
        height: 50px;
        margin-top: 15px;
        line-height: 50px;
      }
      .app-bottom table {
        margin-top: 20px;
        width: 100%;
        border: 2px solid #fff;
        border-collapse: collapse;
      }
      .app-bottom table img {
        width: 60px;
      }
      .app-bottom table tr th {
        /* background-color: blanchedalmond; */
      }
      .app-bottom table tr td {
        text-align: center;
        height: 50px;
      }
      .btnStyle {
        width: 20px;
        height: 20px;
      }
      input {
        background-color: #0000;
        border: 0;
        outline: none;
        border: 2px solid #fff;
        color: #fff;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="app-top">
        <div>
          <span>编号:</span><input type="text" v-model="number" />
          <span>品牌名称:</span><input type="text" v-model="productName" />
        </div>
        <div>
          <span>产品图片地址:</span><input type="text" v-model="imgSrc" /><span
            >价格:</span
          ><input type="text" v-model="price" /><button @click="add()">
            添加
          </button>
        </div>
      </div>
      <div class="app-middle">
          总价格:{{total}}
      </div>
      <div class="app-bottom">
        <table border="1">
          <tr>
            <th>全选:<input type="checkbox" v-model="allCheck"  @change="allChecjChange"/></th>
            <th>产品图片</th>
            <th>编号</th>
            <th>品牌名称</th>
            <th>价格</th>
            <th>数量</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item,index) of products" :key="item.id">
            <!-- 通过定义数组和:value配合 -->
            <td><input type="checkbox" v-model="checks" :value="item"></td>
            <td><img :src="item.imgSrc" alt="" /></td>
            <td>{{item.number}}</td>
            <td style="color: blueviolet">{{item.brandName}}</td>
            <td style="color: red; font-weight: 600; font-size: 30px">
              {{item.price}}
            </td>
            <td style="width: 100px;">
              <button :class="classA" @click="increase(item)">+</button>&nbsp;{{item.count}}&nbsp;<button :class="classA" @click="reduce(item)" :disabled="item.count<=1">-</button>
            </td>
            <td><button @click="del(index)">删除</button></td>
          </tr>
        </table>
        <div style="text-align: center" v-show="!products.length">
          没有更多数据了
        </div>
      </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script>
      //封装localstoreage的方法
      //加入需求
      //计算总价格
      //需要用到computer
      let local = {
        get(key) {
          return JSON.parse(localStorage.getItem(key));
        },
        set(key, value) {
          localStorage.setItem(key, JSON.stringify(value));
        },
      };
      //初始化数据
      let initData = [
        {
          imgSrc:
            "http://t15.baidu.com/it/u=3639416327,658012682&fm=224&app=112&f=JPEG?w=500&h=500",
          number: 1,
          brandName: "美特斯邦威",
          price: 299,
          count: 1,
          id: 1,
        },
        {
          imgSrc:
            "https://img2.baidu.com/it/u=2173973577,3225172081&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=319",
          number: 2,
          brandName: "香奈儿",
          price: 29999,
          count: 3,
          id: 2,
        },
      ];
      let vm = new Vue({
        el: "#app",
        data: {
          number: "",
          productName: "",
          imgSrc: "",
          price: "",
          count:"1",
          classA: "btnStyle",
          products:local.get("aaa-xxx") ?? initData,
          // products:initData,
          //复选框
          checks:[],
          allCheck:false
        },
        computed: {
          total:function(){
            //js处理小数运算精度丢失的问题、转成整数来计算、计算完成除以对应的
              let res =  this.checks.reduce((total,item)=>{
                   return total+=item.price*item.count
               },0)
               return res.toFixed(2)
          }
        },
        watch: {
           //直接侦听数组
           //核心逻辑
           //当products的length与newChecks的length相等时就相当于全选
           //定义一个变量allCheck来控制是否全选？？
           //复选框需要绑定一个[],:value绑定item
           checks(newChecks){
            //  console.log(newChecks)
            if(newChecks.length === this.products.length){
              //全选需要勾起来
              this.allCheck = true
            }else{
              this.allCheck=false
            }
           }
        },
        methods: {
          //全选的勾选状态
          //触发的事件回调
          allChecjChange(){
              console.log(this.allCheck);
              if(this.allCheck){
                //所有的商品都要被勾选
                this.checks = this.products
              }else{
                //取消勾选
                this.checks =[]
              }
          },
          del(index) {
            let target = this.products.splice(index, 1);
            local.set("aaa-xxx",this.products)
            //过滤掉checks数组中id和传入id相同的选项
            //返回checks数组中id和传入id不相同的选项
            this.checks =this.checks.filter(item=>item.id !== target[0].id)
          },
          increase(item){
                 item.count++;
          },
          reduce(item){
              item.count--;
          },
          add() {
            if (
              !this.number ||
              !this.productName ||
              !this.price
            ) {
              return console.log("请输入");
            }
            let obj = {};
            obj.imgSrc = this.imgSrc ? this.imgSrc :"https://img2.baidu.com/it/u=3013320809,2680019834&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672333200&t=a8677c7d10ef4ebb16da124ba0facc65";
            obj.number = this.number;
            obj.brandName = this.productName;
            obj.price = this.price;
            obj.count = this.count;
            obj.id = Math.random().toString(32).substring(2);
            this.products.unshift(obj);
            //将products数据存到本地
            local.set("aaa-xxx",this.products)
          },
        },
      });
    </script>
  </body>
</html>
